{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.635459Z",
     "start_time": "2020-11-17T13:30:33.933790Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "np.set_printoptions(precision=2)\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy import stats\n",
    "from collections import Counter\n",
    "\n",
    "sns.set_style('ticks')\n",
    "\n",
    "%matplotlib inline\n",
    "import warnings\n",
    "warnings.filterwarnings('ignore')\n",
    "import matplotlib as mpl\n",
    "mpl.rcParams['figure.dpi']= 300\n",
    "mpl.rc(\"savefig\", dpi=300)\n",
    "\n",
    "from scipy.special import xlogy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Read files and select drugs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.642438Z",
     "start_time": "2020-11-17T13:30:34.637064Z"
    }
   },
   "outputs": [],
   "source": [
    "ref_type = 'log2_median_ic50_hn' # log2_median_ic50_3f_hn | log2_median_ic50_hn\n",
    "model_name = 'RWEN'\n",
    "\n",
    "dosage_shifted = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.647065Z",
     "start_time": "2020-11-17T13:30:34.644334Z"
    }
   },
   "outputs": [],
   "source": [
    "norm_type = 'cell_TPM'\n",
    "\n",
    "current_dir = '../result/HN_model/{}/'.format(norm_type)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.678950Z",
     "start_time": "2020-11-17T13:30:34.649040Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Drug Name</th>\n",
       "      <th>Synonyms</th>\n",
       "      <th>Target</th>\n",
       "      <th>Target Pathway</th>\n",
       "      <th>Selleckchem Cat#</th>\n",
       "      <th>CAS number</th>\n",
       "      <th>PubCHEM</th>\n",
       "      <th>Others</th>\n",
       "      <th>entropy</th>\n",
       "      <th>max_conc</th>\n",
       "      <th>...</th>\n",
       "      <th>median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_9f</th>\n",
       "      <th>log2_median_ic50_hn</th>\n",
       "      <th>median_ic50_hn</th>\n",
       "      <th>median_ic50_3f_hn</th>\n",
       "      <th>log2_median_ic50_3f_hn</th>\n",
       "      <th>median_ic50_9f_hn</th>\n",
       "      <th>log2_median_ic50_9f_hn</th>\n",
       "      <th>num_sensitive</th>\n",
       "      <th>num_sensitive_hn</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Drug ID</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1001</th>\n",
       "      <td>AICA Ribonucleotide</td>\n",
       "      <td>AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...</td>\n",
       "      <td>AMPK agonist</td>\n",
       "      <td>Metabolism</td>\n",
       "      <td>S1802</td>\n",
       "      <td>2627-69-2</td>\n",
       "      <td>65110</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.034272</td>\n",
       "      <td>2000.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>206.748380</td>\n",
       "      <td>7.691732</td>\n",
       "      <td>9.939784</td>\n",
       "      <td>982.139588</td>\n",
       "      <td>327.379863</td>\n",
       "      <td>8.354822</td>\n",
       "      <td>109.126621</td>\n",
       "      <td>6.769859</td>\n",
       "      <td>476</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1003</th>\n",
       "      <td>Camptothecin</td>\n",
       "      <td>7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...</td>\n",
       "      <td>TOP1</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1288</td>\n",
       "      <td>7689-03-4</td>\n",
       "      <td>104842</td>\n",
       "      <td>(SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...</td>\n",
       "      <td>4.609530</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002003</td>\n",
       "      <td>-8.963413</td>\n",
       "      <td>-7.587491</td>\n",
       "      <td>0.005199</td>\n",
       "      <td>0.001733</td>\n",
       "      <td>-9.172454</td>\n",
       "      <td>0.000578</td>\n",
       "      <td>-10.757416</td>\n",
       "      <td>688</td>\n",
       "      <td>30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1004</th>\n",
       "      <td>Vinblastine</td>\n",
       "      <td>Velban</td>\n",
       "      <td>Microtubule destabiliser</td>\n",
       "      <td>Mitosis</td>\n",
       "      <td>S1248</td>\n",
       "      <td>143-67-9</td>\n",
       "      <td>6710780</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.297122</td>\n",
       "      <td>0.1000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.001599</td>\n",
       "      <td>-9.289051</td>\n",
       "      <td>-7.150982</td>\n",
       "      <td>0.007036</td>\n",
       "      <td>0.002345</td>\n",
       "      <td>-8.735945</td>\n",
       "      <td>0.000782</td>\n",
       "      <td>-10.320907</td>\n",
       "      <td>753</td>\n",
       "      <td>33</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1006</th>\n",
       "      <td>Cytarabine</td>\n",
       "      <td>Ara-Cytidine, Arabinosyl Cytosine, U-19920</td>\n",
       "      <td>Antimetabolite</td>\n",
       "      <td>DNA replication</td>\n",
       "      <td>S1648</td>\n",
       "      <td>147-94-4</td>\n",
       "      <td>6253</td>\n",
       "      <td>NaN</td>\n",
       "      <td>6.646594</td>\n",
       "      <td>2.0000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.163032</td>\n",
       "      <td>-2.616771</td>\n",
       "      <td>-1.342632</td>\n",
       "      <td>0.394301</td>\n",
       "      <td>0.131434</td>\n",
       "      <td>-2.927594</td>\n",
       "      <td>0.043811</td>\n",
       "      <td>-4.512557</td>\n",
       "      <td>508</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1007</th>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>RP-56976, Taxotere</td>\n",
       "      <td>Microtubule stabiliser</td>\n",
       "      <td>Mitosis</td>\n",
       "      <td>S1148</td>\n",
       "      <td>114977-28-5</td>\n",
       "      <td>148124</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.220984</td>\n",
       "      <td>0.0125</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000761</td>\n",
       "      <td>-10.358915</td>\n",
       "      <td>-9.792998</td>\n",
       "      <td>0.001127</td>\n",
       "      <td>0.000376</td>\n",
       "      <td>-11.377960</td>\n",
       "      <td>0.000125</td>\n",
       "      <td>-12.962923</td>\n",
       "      <td>584</td>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   Drug Name  \\\n",
       "Drug ID                        \n",
       "1001     AICA Ribonucleotide   \n",
       "1003            Camptothecin   \n",
       "1004             Vinblastine   \n",
       "1006              Cytarabine   \n",
       "1007               Docetaxel   \n",
       "\n",
       "                                                  Synonyms  \\\n",
       "Drug ID                                                      \n",
       "1001     AICAR, N1-(b-D-Ribofuranosyl)-5-aminoimidazole...   \n",
       "1003     7-Ethyl-10-Hydroxy-Camptothecin, SN-38, Irinot...   \n",
       "1004                                                Velban   \n",
       "1006            Ara-Cytidine, Arabinosyl Cytosine, U-19920   \n",
       "1007                                    RP-56976, Taxotere   \n",
       "\n",
       "                           Target   Target Pathway Selleckchem Cat#  \\\n",
       "Drug ID                                                               \n",
       "1001                 AMPK agonist       Metabolism            S1802   \n",
       "1003                         TOP1  DNA replication            S1288   \n",
       "1004     Microtubule destabiliser          Mitosis            S1248   \n",
       "1006               Antimetabolite  DNA replication            S1648   \n",
       "1007       Microtubule stabiliser          Mitosis            S1148   \n",
       "\n",
       "          CAS number  PubCHEM  \\\n",
       "Drug ID                         \n",
       "1001       2627-69-2    65110   \n",
       "1003       7689-03-4   104842   \n",
       "1004        143-67-9  6710780   \n",
       "1006        147-94-4     6253   \n",
       "1007     114977-28-5   148124   \n",
       "\n",
       "                                                    Others   entropy  \\\n",
       "Drug ID                                                                \n",
       "1001                                                   NaN  6.034272   \n",
       "1003     (SN-38, S4908, 86639-52-3) (Irinotecan, S1198,...  4.609530   \n",
       "1004                                                   NaN  4.297122   \n",
       "1006                                                   NaN  6.646594   \n",
       "1007                                                   NaN  4.220984   \n",
       "\n",
       "          max_conc  ...  median_ic50_9f  log2_median_ic50_9f  \\\n",
       "Drug ID             ...                                        \n",
       "1001     2000.0000  ...      206.748380             7.691732   \n",
       "1003        0.1000  ...        0.002003            -8.963413   \n",
       "1004        0.1000  ...        0.001599            -9.289051   \n",
       "1006        2.0000  ...        0.163032            -2.616771   \n",
       "1007        0.0125  ...        0.000761           -10.358915   \n",
       "\n",
       "         log2_median_ic50_hn  median_ic50_hn  median_ic50_3f_hn  \\\n",
       "Drug ID                                                           \n",
       "1001                9.939784      982.139588         327.379863   \n",
       "1003               -7.587491        0.005199           0.001733   \n",
       "1004               -7.150982        0.007036           0.002345   \n",
       "1006               -1.342632        0.394301           0.131434   \n",
       "1007               -9.792998        0.001127           0.000376   \n",
       "\n",
       "         log2_median_ic50_3f_hn  median_ic50_9f_hn  log2_median_ic50_9f_hn  \\\n",
       "Drug ID                                                                      \n",
       "1001                   8.354822         109.126621                6.769859   \n",
       "1003                  -9.172454           0.000578              -10.757416   \n",
       "1004                  -8.735945           0.000782              -10.320907   \n",
       "1006                  -2.927594           0.043811               -4.512557   \n",
       "1007                 -11.377960           0.000125              -12.962923   \n",
       "\n",
       "         num_sensitive  num_sensitive_hn  \n",
       "Drug ID                                   \n",
       "1001               476                27  \n",
       "1003               688                30  \n",
       "1004               753                33  \n",
       "1006               508                25  \n",
       "1007               584                32  \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_info_df = pd.read_csv('../preprocessed_data/GDSC/hn_drug_stat.csv', index_col=0)\n",
    "drug_info_df.index = drug_info_df.index.astype(str)\n",
    "\n",
    "drug_id_name_dict = dict(zip(drug_info_df.index, drug_info_df['Drug Name'].values))\n",
    "\n",
    "drug_info_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.686052Z",
     "start_time": "2020-11-17T13:30:34.680857Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Docetaxel',\n",
       " 'Doxorubicin',\n",
       " 'Epothilone B',\n",
       " 'Gefitinib',\n",
       " 'Obatoclax Mesylate',\n",
       " 'PHA-793887',\n",
       " 'PI-103',\n",
       " 'Vorinostat']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tested_drug_list = [1007, 133, 201, 1010] + [182, 301, 302] + [1012]\n",
    "[drug_id_name_dict[str(d)] for d in tested_drug_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.710339Z",
     "start_time": "2020-11-17T13:30:34.688183Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id</th>\n",
       "      <th>delta</th>\n",
       "      <th>kill</th>\n",
       "      <th>drug_name</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>133</td>\n",
       "      <td>-3.107634</td>\n",
       "      <td>87.297331</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>1</td>\n",
       "      <td>87.297331</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>201</td>\n",
       "      <td>-2.125949</td>\n",
       "      <td>76.854282</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>1</td>\n",
       "      <td>76.854282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1010</td>\n",
       "      <td>1.480563</td>\n",
       "      <td>33.410070</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>1</td>\n",
       "      <td>33.410070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>182</td>\n",
       "      <td>-1.967096</td>\n",
       "      <td>75.642212</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>1</td>\n",
       "      <td>75.642212</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id     delta       kill           drug_name  cluster_p  \\\n",
       "0   HN120    1007 -0.462341  56.818499           Docetaxel          1   \n",
       "1   HN120     133 -3.107634  87.297331         Doxorubicin          1   \n",
       "2   HN120     201 -2.125949  76.854282        Epothilone B          1   \n",
       "3   HN120    1010  1.480563  33.410070           Gefitinib          1   \n",
       "4   HN120     182 -1.967096  75.642212  Obatoclax Mesylate          1   \n",
       "\n",
       "   cluster_kill  \n",
       "0     56.818499  \n",
       "1     87.297331  \n",
       "2     76.854282  \n",
       "3     33.410070  \n",
       "4     75.642212  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if dosage_shifted:\n",
    "    single_drug_pred_df = pd.read_csv(current_dir + 'pred_drug_kill_{}_{}_shifted.csv'.format(ref_type, model_name))\n",
    "else:\n",
    "    single_drug_pred_df = pd.read_csv(current_dir + 'pred_drug_kill_{}_{}.csv'.format(ref_type, model_name))\n",
    "\n",
    "\n",
    "single_drug_pred_df.loc[:, 'drug_id'] = single_drug_pred_df.loc[:, 'drug_id'].values.astype(str)\n",
    "single_drug_pred_df.loc[:, 'drug_name'] = [drug_id_name_dict[d] for d in single_drug_pred_df.loc[:, 'drug_id'].values]\n",
    "\n",
    "patient_list = sorted(list(set(single_drug_pred_df['patient'])))\n",
    "# sel_drug_id_list = sorted(list(set(single_drug_pred_df['drug_id'])))\n",
    "\n",
    "single_drug_pred_df.loc[:, 'cluster_p'] = 1\n",
    "single_drug_pred_df.loc[:, 'cluster_kill'] = single_drug_pred_df['kill']\n",
    "\n",
    "single_drug_pred_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### List all drug pairs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.725275Z",
     "start_time": "2020-11-17T13:30:34.712364Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(168, 3)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B\n",
       "0   HN120  1007   133\n",
       "1   HN120  1007   201\n",
       "2   HN120  1007  1010\n",
       "3   HN120  1007   182\n",
       "4   HN120  1007   301"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_list = []\n",
    "n_drugs = len(tested_drug_list)\n",
    "\n",
    "for p in patient_list:\n",
    "    for x in range(0, n_drugs-1):\n",
    "        for y in range(x+1, n_drugs):\n",
    "            drug_x = str(tested_drug_list[x])\n",
    "            drug_y = str(tested_drug_list[y])\n",
    "\n",
    "            drug_combi_list += [[p, drug_x, drug_y]]\n",
    "\n",
    "drug_combi_df = pd.DataFrame(drug_combi_list, columns=['patient', 'A', 'B'])\n",
    "\n",
    "print (drug_combi_df.shape)\n",
    "drug_combi_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### Get pred and info for each drug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.742444Z",
     "start_time": "2020-11-17T13:30:34.728302Z"
    }
   },
   "outputs": [],
   "source": [
    "merge_df = pd.merge(drug_combi_df, single_drug_pred_df, how='left', left_on=['patient', 'A'], right_on=['patient', 'drug_id'])\n",
    "drug_combi_pred_df = pd.merge(merge_df, single_drug_pred_df[['patient', 'drug_id', 'drug_name', 'cluster_kill', 'kill']], how='left', left_on=['patient', 'B'], right_on=['patient', 'drug_id'], suffixes=['_A', '_B'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.762205Z",
     "start_time": "2020-11-17T13:30:34.745425Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>delta</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>87.297331</td>\n",
       "      <td>87.297331</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>76.854282</td>\n",
       "      <td>76.854282</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>33.410070</td>\n",
       "      <td>33.410070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>75.642212</td>\n",
       "      <td>75.642212</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>301</td>\n",
       "      <td>PHA-793887</td>\n",
       "      <td>88.277359</td>\n",
       "      <td>88.277359</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B drug_id_A     delta     kill_A drug_name_A  cluster_p  \\\n",
       "0   HN120  1007   133      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "1   HN120  1007   201      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "2   HN120  1007  1010      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "3   HN120  1007   182      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "4   HN120  1007   301      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "\n",
       "   cluster_kill_A drug_id_B         drug_name_B  cluster_kill_B     kill_B  \n",
       "0       56.818499       133         Doxorubicin       87.297331  87.297331  \n",
       "1       56.818499       201        Epothilone B       76.854282  76.854282  \n",
       "2       56.818499      1010           Gefitinib       33.410070  33.410070  \n",
       "3       56.818499       182  Obatoclax Mesylate       75.642212  75.642212  \n",
       "4       56.818499       301          PHA-793887       88.277359  88.277359  "
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.802707Z",
     "start_time": "2020-11-17T13:30:34.765360Z"
    }
   },
   "outputs": [],
   "source": [
    "rows = []\n",
    "for _, data in drug_combi_pred_df.iterrows():\n",
    "\n",
    "    \n",
    "    cluster_kill_A = data['cluster_kill_A']\n",
    "    cluster_kill_B = data['cluster_kill_B']\n",
    "    cluster_kill_C = cluster_kill_A + cluster_kill_B - np.multiply(cluster_kill_A/100, cluster_kill_B/100)*100\n",
    "    kill_C = cluster_kill_C\n",
    "    \n",
    "    best_kill = np.max([data['kill_A'], data['kill_B']])\n",
    "    improve = kill_C - best_kill\n",
    "    improve_p = (kill_C - best_kill) / best_kill\n",
    "    \n",
    "    sum_kill_dif = np.sum(np.abs(cluster_kill_A - cluster_kill_B))\n",
    "    \n",
    "    ##### save output #####\n",
    "    \n",
    "    rows += [[kill_C, improve, improve_p, sum_kill_dif]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.828247Z",
     "start_time": "2020-11-17T13:30:34.805776Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>delta</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_B</th>\n",
       "      <th>kill_C</th>\n",
       "      <th>improve</th>\n",
       "      <th>improve_p</th>\n",
       "      <th>sum_kill_dif</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>133</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>87.297331</td>\n",
       "      <td>87.297331</td>\n",
       "      <td>94.514797</td>\n",
       "      <td>7.217466</td>\n",
       "      <td>0.082677</td>\n",
       "      <td>30.478832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>201</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>76.854282</td>\n",
       "      <td>76.854282</td>\n",
       "      <td>90.005331</td>\n",
       "      <td>13.151050</td>\n",
       "      <td>0.171117</td>\n",
       "      <td>20.035783</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>1010</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>33.410070</td>\n",
       "      <td>33.410070</td>\n",
       "      <td>71.245469</td>\n",
       "      <td>14.426970</td>\n",
       "      <td>0.253913</td>\n",
       "      <td>23.408429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>182</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>75.642212</td>\n",
       "      <td>75.642212</td>\n",
       "      <td>89.481941</td>\n",
       "      <td>13.839730</td>\n",
       "      <td>0.182963</td>\n",
       "      <td>18.823713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>301</td>\n",
       "      <td>1007</td>\n",
       "      <td>-0.462341</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>301</td>\n",
       "      <td>PHA-793887</td>\n",
       "      <td>88.277359</td>\n",
       "      <td>88.277359</td>\n",
       "      <td>94.937988</td>\n",
       "      <td>6.660629</td>\n",
       "      <td>0.075451</td>\n",
       "      <td>31.458860</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient     A     B drug_id_A     delta     kill_A drug_name_A  cluster_p  \\\n",
       "0   HN120  1007   133      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "1   HN120  1007   201      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "2   HN120  1007  1010      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "3   HN120  1007   182      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "4   HN120  1007   301      1007 -0.462341  56.818499   Docetaxel          1   \n",
       "\n",
       "   cluster_kill_A drug_id_B         drug_name_B  cluster_kill_B     kill_B  \\\n",
       "0       56.818499       133         Doxorubicin       87.297331  87.297331   \n",
       "1       56.818499       201        Epothilone B       76.854282  76.854282   \n",
       "2       56.818499      1010           Gefitinib       33.410070  33.410070   \n",
       "3       56.818499       182  Obatoclax Mesylate       75.642212  75.642212   \n",
       "4       56.818499       301          PHA-793887       88.277359  88.277359   \n",
       "\n",
       "      kill_C    improve  improve_p  sum_kill_dif  \n",
       "0  94.514797   7.217466   0.082677     30.478832  \n",
       "1  90.005331  13.151050   0.171117     20.035783  \n",
       "2  71.245469  14.426970   0.253913     23.408429  \n",
       "3  89.481941  13.839730   0.182963     18.823713  \n",
       "4  94.937988   6.660629   0.075451     31.458860  "
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = pd.concat([drug_combi_pred_df, pd.DataFrame(rows, columns=['kill_C', 'improve', 'improve_p', 'sum_kill_dif'])], axis=1)\n",
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.848753Z",
     "start_time": "2020-11-17T13:30:34.829907Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patient</th>\n",
       "      <th>drug_id_A</th>\n",
       "      <th>drug_name_A</th>\n",
       "      <th>drug_id_B</th>\n",
       "      <th>drug_name_B</th>\n",
       "      <th>cluster_p</th>\n",
       "      <th>cluster_kill_A</th>\n",
       "      <th>cluster_kill_B</th>\n",
       "      <th>kill_A</th>\n",
       "      <th>kill_B</th>\n",
       "      <th>kill_C</th>\n",
       "      <th>improve</th>\n",
       "      <th>improve_p</th>\n",
       "      <th>sum_kill_dif</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>133</td>\n",
       "      <td>Doxorubicin</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>87.297331</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>87.297331</td>\n",
       "      <td>94.514797</td>\n",
       "      <td>7.217466</td>\n",
       "      <td>0.082677</td>\n",
       "      <td>30.478832</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>201</td>\n",
       "      <td>Epothilone B</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>76.854282</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>76.854282</td>\n",
       "      <td>90.005331</td>\n",
       "      <td>13.151050</td>\n",
       "      <td>0.171117</td>\n",
       "      <td>20.035783</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>1010</td>\n",
       "      <td>Gefitinib</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>33.410070</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>33.410070</td>\n",
       "      <td>71.245469</td>\n",
       "      <td>14.426970</td>\n",
       "      <td>0.253913</td>\n",
       "      <td>23.408429</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>182</td>\n",
       "      <td>Obatoclax Mesylate</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>75.642212</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>75.642212</td>\n",
       "      <td>89.481941</td>\n",
       "      <td>13.839730</td>\n",
       "      <td>0.182963</td>\n",
       "      <td>18.823713</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>HN120</td>\n",
       "      <td>1007</td>\n",
       "      <td>Docetaxel</td>\n",
       "      <td>301</td>\n",
       "      <td>PHA-793887</td>\n",
       "      <td>1</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>88.277359</td>\n",
       "      <td>56.818499</td>\n",
       "      <td>88.277359</td>\n",
       "      <td>94.937988</td>\n",
       "      <td>6.660629</td>\n",
       "      <td>0.075451</td>\n",
       "      <td>31.458860</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  patient drug_id_A drug_name_A drug_id_B         drug_name_B  cluster_p  \\\n",
       "0   HN120      1007   Docetaxel       133         Doxorubicin          1   \n",
       "1   HN120      1007   Docetaxel       201        Epothilone B          1   \n",
       "2   HN120      1007   Docetaxel      1010           Gefitinib          1   \n",
       "3   HN120      1007   Docetaxel       182  Obatoclax Mesylate          1   \n",
       "4   HN120      1007   Docetaxel       301          PHA-793887          1   \n",
       "\n",
       "   cluster_kill_A  cluster_kill_B     kill_A     kill_B     kill_C    improve  \\\n",
       "0       56.818499       87.297331  56.818499  87.297331  94.514797   7.217466   \n",
       "1       56.818499       76.854282  56.818499  76.854282  90.005331  13.151050   \n",
       "2       56.818499       33.410070  56.818499  33.410070  71.245469  14.426970   \n",
       "3       56.818499       75.642212  56.818499  75.642212  89.481941  13.839730   \n",
       "4       56.818499       88.277359  56.818499  88.277359  94.937988   6.660629   \n",
       "\n",
       "   improve_p  sum_kill_dif  \n",
       "0   0.082677     30.478832  \n",
       "1   0.171117     20.035783  \n",
       "2   0.253913     23.408429  \n",
       "3   0.182963     18.823713  \n",
       "4   0.075451     31.458860  "
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "drug_combi_pred_df = drug_combi_pred_df[['patient', 'drug_id_A', 'drug_name_A', 'drug_id_B', 'drug_name_B', 'cluster_p', 'cluster_kill_A', 'cluster_kill_B', 'kill_A', 'kill_B', 'kill_C', 'improve', 'improve_p', 'sum_kill_dif']]\n",
    "\n",
    "drug_combi_pred_df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-11-17T13:30:34.865274Z",
     "start_time": "2020-11-17T13:30:34.851621Z"
    }
   },
   "outputs": [],
   "source": [
    "if dosage_shifted:\n",
    "    drug_combi_pred_df.to_csv(current_dir + 'pred_combi_kill_{}_{}_shifted.csv'.format(ref_type, model_name), index=False)\n",
    "else:\n",
    "    drug_combi_pred_df.to_csv(current_dir + 'pred_combi_kill_{}_{}.csv'.format(ref_type, model_name), index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
